package controllers;

import beans.InhoudBean1;
import connectivity.Dbmanager;
import connectivity.QueryManager;
import java.io.*;
import java.util.LinkedList;
import javax.servlet.*;
import javax.servlet.http.*;
import models.Course;
import models.Module;
import tools.Checker;

public class ChangeModuleController extends HttpServlet {

    private Dbmanager dbManager;
    private QueryManager queryManager;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        if (request.getParameter("moduledelete") != null) {
           doPost(request, response);
        }

        dbManager = new Dbmanager();
        dbManager.openConnection();
        queryManager = new QueryManager(dbManager);

        Module module = new Module();
        int moduleid = Integer.parseInt(request.getParameter("moduleident"));
        module = queryManager.getModuleById(moduleid);

        dbManager.closeConnection();
        request.setAttribute("module", module);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/change_module.jsp");
        dispatcher.forward(request, response);

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        dbManager = new Dbmanager();
        dbManager.openConnection();
        queryManager = new QueryManager(dbManager);
        String moduleName = "", moduleDescription = "", address = "";
        boolean isEmpty = false;
        int moduleID;
        Module module = new Module();
        HttpSession sessie = request.getSession();
        InhoudBean1 inhoud = (InhoudBean1) sessie.getAttribute("inhoud");

        moduleID = Integer.parseInt(request.getParameter("moduleid"));
        module.setModuleID(moduleID);

        if (!request.getParameter("moduleName").isEmpty()) {
            moduleName = request.getParameter("moduleName");
            module.setModuleName(moduleName);
            request.setAttribute("moduleName", moduleName);
        } else {
            isEmpty = true;
            request.setAttribute("foutMelding", inhoud.getError1());
        }
        if (!request.getParameter("moduleDescription").isEmpty()) {
            moduleDescription = request.getParameter("moduleDescription");
            request.setAttribute("moduleDescription", moduleDescription);
            module.setModuleDescription(moduleDescription);
        } else {
            isEmpty = true;
            request.setAttribute("foutMelding", inhoud.getError2());
        }


        if (!isEmpty) {
            queryManager.updateModule(module);
            address = "subscribe_to_course";
        } else {
            request.setAttribute("module", module);
            address = "/change_module.jsp";

        }
        dbManager.closeConnection();
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);

        dispatcher.forward(request, response);
    }
}
